package com.jf3q.admin.controller;

import com.alibaba.excel.EasyExcel;
import com.jf3q.admin.excel.SubjectData;
import com.jf3q.admin.excel.SubjectDataListener;
import com.jf3q.admin.service.impl.SubjectServiceImpl;
import com.jf3q.admin.utils.vo.AjaxResultVO;
import com.jf3q.common.domain.EduSubject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ASUS
 * @date 2024/4/16
 * @apiNote
 */
@RestController
@RequestMapping("/subject")
public class SubjectController {

    @Autowired
    SubjectServiceImpl subjectService;

    @GetMapping("/getTree")
    public AjaxResultVO getTree() {
        List<EduSubject> list = subjectService.list();
        List<EduSubject> tree = new ArrayList<>();
        for (EduSubject subject : list) {
            if (subject.getParentId().equals("0")) {
                for (EduSubject subject1 : list) {
                    if (subject1.getParentId().equals(subject.getId())) {
                        subject.getSubjects().add(subject1);
                    }
                }
                tree.add(subject);
            }
        }
        return AjaxResultVO.success(tree);
    }

    @PostMapping("/excel")
    public AjaxResultVO excel(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), SubjectData.class, new SubjectDataListener(subjectService)).sheet().doRead();
        return AjaxResultVO.success();
    }
}
